package org.display.util
{
	import flash.filesystem.File;
	import flash.utils.*;
	
	import org.data.*;
	import org.data.HashMap;
	import org.net.*;
	import org.utils.*;
	
	
	public class PublicAllJsfl
	{

		var items:Array=[]	
		
		var jsfls:Array=[]
			
		var paths:Array=[]
			
		var maps:HashMap
		
		public function PublicAllJsfl()
		{

			maps=new HashMap()
			loadAllxml()
			
		}
		
		//第一步导入所有xml
		function loadAllxml(){
			
			FileControl.readFile(paths,Global.workspace+"\\xml")
			
			loadxml(paths.concat())
			
		}
		
		function loadxml(paths:Array){
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				maps.put(path,e)
				
				items.push({path:path,dopath:path.replace(/\\/gis,"/").replace(/.*?(?=\/xml\/)/,""),sub:getChildren(path).join(",")})
				
				//trace(getChildren(path).join(","))	
				
				if(paths.length>0){
					
					loadxml(paths)
					
				}else{
					
					jsfls=ArrayControl.orderXML(items)

					//trace(jsfls)
					
					if(jsfls.length>0)doJsfl(jsfls.shift().path)
					
				}
				
			}
			
			res.onFault=function(e,path){
				
				trace(e,path)
				
			}
			
			var xmlpath:String=paths.shift().replace(/file:\/\/\//,"")
			
			new AbcHTTPServices(xmlpath,res)
			
		}
		
		
		//获取一个模块所有的子模块
		function getChildren(path:String):Array{
			
			var childs:Array=[]
			
			createItem(path)
			
			childs.reverse()
			
			return childs
			
			function createItem(path:String){
				
				var source:XML=XML(maps.getValue(path))
				
				childs.push(path)
				
				var values:XMLList=source.item
				
				for(var i=0;i<values.length();i++){
					
				//	trace(values[i].path+"/////////////")
					
					if(values[i].path!=undefined){
						
						createItem((Global.workspace+values[i].path.toString()).replace(/\\/gis,"/"))
						
					}
					
				}
				
			}
		}
		
		
		
		function doJsfl(path:String){
			
			var jsflPath:String=path.replace(/\//gis,"\\").replace(/\\xml\\/gis,"\\jsfl\\").replace(".xml",".jsfl")
			
			//trace(jsflPath+"???????????????????")
			
			var file:File = File.desktopDirectory.resolvePath(jsflPath);
			file.openWithDefaultApplication();
			
			if(jsfls.length>0)setTimeout(doJsfl,2000,jsfls.shift().path)
			
		}
		
		
	}
}